package chapter_5;

import java.util.Random;

/**
 * (HairAssistant). 
 *
 * @author 汪文波(Wang Wenbo) wenboit@163.com
 * @notes Create on 2021-12-20 07:56
 */
public class HairAssistant {

    public static void main(String[] args) {
        timeCost(incrementArr(10000));
        System.out.println();
        timeCost(decrementArr(10000));
        System.out.println();
        timeCost(randomArr(10000));

    }

    public static void hire(int[] arr) {
        System.out.format("Candidate count: %d\n", arr.length);
        int best = arr[0];
        int changeNum = 0;
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > best) {
                best = arr[i];
                changeNum++;
            }
        }
        System.out.format("Change Count: %d\n", changeNum);
    }

    private static int[] incrementArr(int length) {
        int[] arr = new int[length];
        for (int i = 0; i < length; i++) {
            arr[i] = i;
        }

        return arr;
    }

    private static int[] decrementArr(int length) {
        int[] arr = new int[length];
        for (int i = 0; i < length; i++) {
            arr[i] = length - i;
        }

        return arr;
    }

    private static int[] randomArr(int length) {
        Random random = new Random();

        int[] arr = new int[length];
        for (int i = 0; i < length; i++) {
            arr[i] = random.nextInt();
        }

        return arr;
    }

    private static void timeCost(int[] arr) {
        long startTime = System.nanoTime();
        hire(arr);
        long endTime = System.nanoTime();
        long delta = endTime - startTime;
        System.out.format("time cost: %dus\n", delta / 1000);

    }

}
